<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>智慧社区后台</title>
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/AdminLTE.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/plugins/bootstrap-table/css/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap-switch/css/bootstrap-switch.min.css" />
</head>
<style>
</style>
<body style="padding-bottom: 90px; background-color: #ECF0F5;">
	<section class="content" style="margin: 15px; background-color: white;">
		<div class="row">
			<div class="col-sm-12">
				<table id="table">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field="sName">服务名称</th>
							<th class="text-center" data-field="s2">副标题</th>
							<th class="text-center" data-field="sCode">唯一编号</th>
							<th class="text-center" data-field="sState">状态</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
		<div class="row">
			<div class="col-sm-12">
				<table id="table1">
					<thead style="background-color: #00C0EF">
						<tr style="background-color: #00C0EF">
							<th class="text-center" data-field="name">名称</th>
							<th class="text-center" data-field="s1">副标题</th>
							<th class="text-center" data-field="code">唯一编号</th>
							<th class="text-center" data-field="type">所属类型</th>
							<th class="text-center" data-field="state">状态</th>
							<th class="text-center" data-field="id">操作</th>
						</tr>
					</thead>
				</table>
			</div>
		</div>
	</section>
	<!-- Large modal -->
	<div id="modal" class="modal fade" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">添加</h4>
				</div>
				<div class="modal-body">
					<div class="input-group">
						<span class="input-group-addon">服务名称：</span> <input type="text" class="form-control" id="dom1" value="" aria-describedby="basic-addon1">
					</div>
					<br>
					<div class="input-group">
						<span class="input-group-addon">唯一编号：</span> <input type="text" class="form-control" id="dom2" value="" aria-describedby="basic-addon1">
					</div>
					<br>
					<div class="input-group">
						<span class="input-group-addon">副标题：</span> <input type="text" class="form-control" id="dom3" value="" aria-describedby="basic-addon1">
					</div>
					<br>
					<div class="form-group">
						<label for="recruit_info_theme" class="control-label">标题图片:</label>
						<button id="imgBtn" class="btn btn-primary glyphicon glyphicon-plus" onclick="imgFile.click();">上传图片</button>
						<img id="image" class="cover-radius" width="150px;" style="cursor: pointer;" onclick="imgFile.click();" /> <input type="file" class="form-control" style="display: none;" name="imgFile" id="imgFile" onchange="fileChange()">
					</div>
				</div>
				<div class="modal-footer">
					<button id="submitBtn" type="button" class="btn btn-primary" onclick="adopt();">提交</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>

	<!-- Large modal -->
	<div id="modal1" class="modal fade" role="dialog" aria-labelledby="myLargeModalLabel">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<span aria-hidden="true">&times;</span>
					</button>
					<h4 class="modal-title">添加</h4>
				</div>
				<div class="modal-body">
					<div class="input-group">
						<span class="input-group-addon">名称：</span> <input type="text" class="form-control" id="name" value="">
					</div>
					<br>
					<div class="input-group">
						<span class="input-group-addon">唯一编号：</span> <input type="text" class="form-control" id="code" value="">
					</div>
					<br>
					<div class="input-group">
						<span class="input-group-addon">副标题：</span> <input type="text" class="form-control" id="s1" value="">
					</div>
					<br>
					<div class="input-group">
						<span class="input-group-addon">所属类型：</span> <select id="type" class="form-control">
							<option value="">请选择...</option>
						</select>
					</div>
					<br>
					<div class="form-group">
						<label for="recruit_info_theme" class="control-label">标题图片:</label>
						<button id="imgBtn1" class="btn btn-primary glyphicon glyphicon-plus" onclick="imgFile1.click();">上传图片</button>
						<img id="image1" class="cover-radius" width="150px;" style="cursor: pointer;" onclick="imgFile1.click();" /> <input type="file" class="form-control" style="display: none;" name="imgFile1" id="imgFile1" onchange="fileChange1()">
					</div>
				</div>
				<div class="modal-footer">
					<button id="submitBtn1" type="button" class="btn btn-primary" onclick="adopt1();">提交</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>
</body>
<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-zh-CN.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-export.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/tableExport.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-toolbar.js"></script>
<script src="${pageContext.request.contextPath}/plugins/bootstrap-table/js/bootstrap-table-filter-control.js"></script>
<script src="${pageContext.request.contextPath}/js/ajaxfileupload.js"></script>
<!-- layer -->
<script src="${pageContext.request.contextPath}/plugins/layer/2.4/layer.js"></script>
<script>
	var imgUrl = "";
	var imgUrl1 = "";
	function switchFormatter(value, row, index) {
		if (row.sState == 0) {
			return [ "<div  class='switch switch-mini'><input id='"+row.sid+"' name='switch-animate' type='checkbox' /></div>" ].join('');
		} else {
			return [ "<div class='switch switch-mini'><input id='"+row.sid+"' name='switch-animate' type='checkbox'  checked/></div>" ].join('');
		}

	}
	function actionFormatter(value, row, index) {
		return "<button class='btn btn-danger btn-xs like' onclick='del(\"" + row.sCode + "\");'>删除</button>";
	}

	var table_dataset = [ {
		field : 'sName',
		align : 'center',
	}, {
		field : 's2',
		align : 'center',
		formatter : function formatter(value, row, index) {
			var str = '无';
			if (row.s2 != null & row.s2 != '') {
				str = row.s2;
			}
			return str;
		}
	}, {
		field : 'sCode',
		align : 'center',
	}, {
		field : 'sState',
		align : 'center',
		formatter : switchFormatter
	}, {
		align : 'center',
		width : 300,
		formatter : actionFormatter,
	} ];
	$('#table').bootstrapTable({
		url : "${pageContext.request.contextPath}/civilian/seleteCivilianPage",
		idField : "sid",
		columns : table_dataset,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ],
		onLoadSuccess : function(data) {
			//console.log(data);
			$('[name="switch-animate"]').bootstrapSwitch({
				onText : "启用",
				offText : "禁用",
				onColor : "info",
				offColor : "danger",
				size : "mini",
				onSwitchChange : function(event, state) {
					//var sid=$(event).parent().prevObject["0"].currentTarget.id;
					var sid = event.currentTarget.id;
					var sState = 0;
					if (state) {
						sState = 1
					}
					/* console.log(event);
					console.log(event.currentTarget.id); */

					$.ajax({
						url : "${pageContext.request.contextPath}/civilian/updateCivilian",
						type : "post",
						data : {
							sid : sid,
							sState : sState
						},
						dataType : "json",
						success : function(data) {
							var result = data['result'];
							if (result === 1) {
								/* layer.msg('操作成功!', {
									icon : 1,
									time : 800
								}); */
							} else {
								/* layer.msg('操作失败!', {
									icon : 2,
									time : 800
								}); */
								$("#" + sid + "").bootstrapSwitch('state', !state, true);

							}
						},
						error : function() {
							layer.msg('系统繁忙，请稍后再试!', {
								icon : 2,
								time : 800
							});
							$("#" + sid + "").bootstrapSwitch('state', !state, true);
						}
					});

				}
			});
		}
	});

	function switchFormatter1(value, row, index) {
		if (row.state == 0) {
			return [ "<div  class='switch switch-mini'><input id='"+row.id+"' name='switch-animate' type='checkbox' /></div>" ].join('');
		} else {
			return [ "<div class='switch switch-mini'><input id='"+row.id+"' name='switch-animate' type='checkbox'  checked/></div>" ].join('');
		}

	}
	function actionFormatter1(value, row, index) {
		return "<button class='btn btn-danger btn-xs like' onclick='del1(\"" + row.id + "\");'>删除</button>";
	}

	var table_dataset1 = [ {
		field : 'name',
		align : 'center',
	}, {
		field : 's1',
		align : 'center',
		formatter : function formatter(value, row, index) {
			var str = '无';
			if (row.s1 != null & row.s1 != '') {
				str = row.s1;
			}
			return str;
		}
	}, {
		field : 'code',
		align : 'center',
	}, {
		field : 'sName',
		align : 'center',
	}, {
		field : 'state',
		align : 'center',
		formatter : switchFormatter1
	}, {
		align : 'center',
		width : 300,
		formatter : actionFormatter1,
	} ];

	$('#table1').bootstrapTable({
		url : "${pageContext.request.contextPath}/civilian/seleteCivilianTypePage",
		idField : "id",
		columns : table_dataset1,
		striped : true, //是否显示行间隔色
		pagination : true, //是否显示分页
		pageSize : 10, //每页的记录行数
		pageList : [ 10, 25, 50, 100 ], //可供选择的每页的行数
		search : false, //是否显示搜索
		showRefresh : true,
		sidePagination : "server", //表示服务端请求  
		queryParamsType : "limit",
		queryParams : function queryParams(params) { //设置查询参数  
			var param = {
				offset : params.offset,
				limit : params.limit
			};
			return param;
		},
		toolbar : "<button class='btn btn-primary glyphicon glyphicon-plus' onclick='showModal1(\"" + '' + "\");'>新增数据</button>",
		// 						+"&nbsp;<button class='btn btn-danger glyphicon glyphicon-trash'>批量删除</button>",
		icons : {
			paginationSwitchDown : 'glyphicon-collapse-down icon-chevron-down',
			paginationSwitchUp : 'glyphicon-collapse-up icon-chevron-up',
			refresh : 'glyphicon-refresh icon-refresh',
			toggle : 'glyphicon-list-alt icon-list-alt',
			columns : 'glyphicon-th icon-th',
			detailOpen : 'glyphicon-plus icon-plus',
			detailClose : 'glyphicon-minus icon-minus',
			"export" : 'glyphicon-export icon-share'
		},
		showExport : true,
		exportDataType : 'basic',
		exportTypes : [ 'json', 'xml', 'csv', 'txt', 'sql', 'excel' ],
		onLoadSuccess : function(data) {
			//console.log(data);
			$('[name="switch-animate"]').bootstrapSwitch({
				onText : "启用",
				offText : "禁用",
				onColor : "info",
				offColor : "danger",
				size : "mini",
				onSwitchChange : function(event, state) {
					//var sid=$(event).parent().prevObject["0"].currentTarget.id;
					var id = event.currentTarget.id;
					var stateStr = 0;
					if (state) {
						stateStr = 1
					}
					/* console.log(event);
					console.log(event.currentTarget.id); */

					$.ajax({
						url : "${pageContext.request.contextPath}/civilian/updateCivilianType",
						type : "post",
						data : {
							id : id,
							state : stateStr
						},
						dataType : "json",
						success : function(data) {
							var result = data['result'];
							if (result === 1) {
								/* layer.msg('操作成功!', {
									icon : 1,
									time : 800
								}); */
							} else {
								/* layer.msg('操作失败!', {
									icon : 2,
									time : 800
								}); */
								$("#" + id + "").bootstrapSwitch('state', !state, true);

							}
						},
						error : function() {
							layer.msg('系统繁忙，请稍后再试!', {
								icon : 2,
								time : 800
							});
							$("#" + id + "").bootstrapSwitch('state', !state, true);
						}
					});

				}
			});
		}
	});

	$(function() {
		selectType();
	});

	function selectType() {
		$.ajax({
			url : "${pageContext.request.contextPath}/civilian/seleteCivilianAll",
			type : "post",
			dataType : "json",
			success : function(data) {
				var list = data['list'];
				var types = "<option value=''>请选择...</option>";
				for (var i = 0; i < list.length; i++) {
					types += "<option value='"+list[i].sCode+"'>" + list[i].sName + "</option>";
				}
				$("#type").html(types);
			}
		});
	}

	function showModal() {
		var element = document.getElementById('image');
		element.src = "";
		imgUrl = "";
		$("#image").hide();
		$("#imgBtn").show();
		$("#dom1").val('');
		$("#dom2").val('');
		$("#dom3").val('');
		$('#modal').modal({
			backdrop : 'static',
			keyboard : false
		});
	}

	function showModal1() {
		var element = document.getElementById('image1');
		element.src = "";
		imgUrl1 = "";
		$("#image1").hide();
		$("#imgBtn1").show();
		$("#name").val('');
		$("#code").val('');
		$("#type").val('');
		$("#s1").val('');
		$('#modal1').modal({
			backdrop : 'static',
			keyboard : false
		});
	}

	function adopt() {
		$("#submitBtn").button('loading');
		var sName = $("#dom1").val();
		var sCode = $("#dom2").val();
		var s2 = $("#dom3").val();
		var url = imgUrl;
		if (sName === '' || sCode === '' || url === '') {
			layer.alert("新增的数据中填写不完整!");
			$("#submitBtn").button('reset');
			return false;
		}
		$.ajax({
			url : "${pageContext.request.contextPath}/civilian/insertCivilian",
			type : "post",
			data : {
				sName : sName,
				sCode : sCode,
				s1 : url,
				s2 : s2
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table").bootstrapTable('refresh');
						$("#modal").modal("hide");
					}, 1000);
					selectType();
				} else if (result === 2) {
					layer.msg('唯一编号重复!', {
						icon : 2,
						time : 800
					});
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
				$("#submitBtn").button('reset');
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
				$("#submitBtn").button('reset');
			}
		});
	}

	function adopt1() {
		$("#submitBtn1").button('loading');
		var name = $("#name").val();
		var code = $("#code").val();
		var type = $("#type").val();
		var s1 = $("#s1").val();
		var url = imgUrl1;
		if (name === '' || code === '' || url === '') {
			layer.alert("新增的数据中填写不完整!");
			$("#submitBtn1").button('reset');
			return false;
		}
		$.ajax({
			url : "${pageContext.request.contextPath}/civilian/insertCivilianType",
			type : "post",
			data : {
				name : name,
				code : code,
				type : type,
				picture : url,
				s1 : s1
			},
			dataType : "json",
			success : function(data) {
				var result = data['result'];
				if (result === 1) {
					layer.msg('操作成功!', {
						icon : 1,
						time : 800
					});
					setTimeout(function() {
						$("#table1").bootstrapTable('refresh');
						$("#modal1").modal("hide");
					}, 1000);
				} else if (result === 2) {
					layer.msg('唯一编号重复!', {
						icon : 2,
						time : 800
					});
				} else {
					layer.msg('操作失败!', {
						icon : 2,
						time : 800
					});
				}
				$("#submitBtn1").button('reset');
			},
			error : function() {
				layer.msg('系统繁忙，请稍后再试!', {
					icon : 2,
					time : 800
				});
				$("#submitBtn1").button('reset');
			}
		});
	}

	function del(code) {
		layer.confirm("是否删除此项？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/civilian/deleteCivilian",
				type : "post",
				data : {
					code : code
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table").bootstrapTable('refresh');
							$("#table1").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}

	function del1(id) {
		layer.confirm("是否删除此项？", function(index) {
			$.ajax({
				url : "${pageContext.request.contextPath}/civilian/deleteCivilianType",
				type : "post",
				data : {
					id : id
				},
				dataType : "json",
				success : function(data) {
					if (data.result === 1) {
						layer.msg('操作成功!', {
							icon : 1,
							time : 800
						});
						setTimeout(function() {
							$("#table1").bootstrapTable('refresh');
						}, 500);
					} else {
						layer.msg('操作失败!', {
							icon : 2,
							time : 800
						});
					}
				},
				error : function() {
					layer.msg('系统繁忙，请稍后再试!', {
						icon : 5,
						time : 800
					});
				}
			});
		});
	}

	function fileChange() {
		//document.getElementById("upload_file_img").value = document.getElementById("imgFile").value;
		if (image_check('imgFile')) { //自己添加的文件后缀名的验证
			$.ajaxFileUpload({
				fileElementId : 'imgFile', //需要上传的文件域的ID，即<input type="file">的ID。
				url : "${pageContext.request.contextPath}/upload_img", //后台方法的路径
				type : 'post', //当要提交自定义参数时，这个参数要设置成post
				dataType : 'json', //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
				secureuri : false, //是否启用安全提交，默认为false。
				async : true, //是否是异步
				success : function(data) { //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
					imgUrl = data.fileName;
					$('#image').attr('src', '${pageContext.request.contextPath}/' + data.url);
					$("#image").show();
					$("#imgBtn").hide();
				}
			});
		}
	}

	function fileChange1() {
		//document.getElementById("upload_file_img").value = document.getElementById("imgFile").value;
		if (image_check('imgFile1')) { //自己添加的文件后缀名的验证
			$.ajaxFileUpload({
				fileElementId : 'imgFile1', //需要上传的文件域的ID，即<input type="file">的ID。
				url : "${pageContext.request.contextPath}/upload_img1", //后台方法的路径
				type : 'post', //当要提交自定义参数时，这个参数要设置成post
				dataType : 'json', //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断。
				secureuri : false, //是否启用安全提交，默认为false。
				async : true, //是否是异步
				success : function(data) { //提交成功后自动执行的处理函数，参数data就是服务器返回的数据。
					imgUrl1 = data.fileName;
					$('#image1').attr('src', '${pageContext.request.contextPath}/' + data.url);
					$("#image1").show();
					$("#imgBtn1").hide();
				}
			});
		}
	}

	function image_check(feid) { //自己添加的文件后缀名的验证
		var img = document.getElementById(feid);
		return /.(jpg|png|gif|bmp)$/.test(img.value) ? true : (function() {
			layer.alert('图片格式仅支持jpg、png、gif、bmp格式，且区分大小写。');
			return false;
		})();
	}
</script>
</html>
